package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuel2;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuelActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CHEF_DUEL2_GET_TABLE_INFO, desc = "获取开宴会的玩家的家")
public class ChefDuel2GetCookerTableCmd extends AbstractCommand {

	int activityId;
	long userId;
	int page;
	int num;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel2UserIdTableReqMsg reqMsg = ChefDuelProto.ChefDuel2UserIdTableReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		userId = reqMsg.getUserId();
		page = reqMsg.getPage();
		num = reqMsg.getNum();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo == null){
			OtherActivityChefDuel2 cookBoy2 = OtherActivityChefDuelActivityMgr.getOtherActivityChefDuel2(activityId);
			if(cookBoy2 != null){
				activityInfo = cookBoy2.getActivityInfo();
			}
		}
		if(activityInfo == null){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		if(userId == player.getUserId()){
			player.getModule(ChefDuelModule.class).sendForbidSpoonIdSet(activityId);
		}
		//重置
		activityId = activityInfo.getActivityId();
		ChefDuelMgr.sendCookerHomeMsg(activityId,player.getUserId(),userId,page,num);
		ChefDuelMgr.addChefDuel2SceneUser(activityId,player.getUserId(),userId);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			ChefDuelProto.ChefDuel2UserIdTableRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2UserIdTableRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CHEF_DUEL2_GET_TABLE_INFO, respMsg);
		}
	}
}
